"""
@Project :auto-phone
@File    :shipin.py
@IDE     :PyCharm
@Author  :zbb
@Date    :2023/10/8 11:41
@description : 拼多多的视频刷脚本
"""
from uiautomator2 import Device
import time
import random


class AutoScanVideo:
    APP_NAME = "com.xunmeng.pinduoduo"
    VIDEO_XPATH = '//*[@resource-id="android:id/content"]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[2]'

    def __init__(self, device):
        self.device: Device = device

    def run(self):
        installed = self.check_install()
        if installed:
            self.init_app()
            self.run_task()
            self.close_app()

    def check_install(self):
        try:
            app_info = self.device.app_info(self.APP_NAME)
        except Exception as e:
            print("在模拟器上未找到app")
            return False
        else:
            return True

    def init_app(self):
        self.device.app_start(self.APP_NAME)
        time.sleep(2)

        # 点击视频
        self.device.xpath(self.VIDEO_XPATH).click()
        time.sleep(2)

    def close_app(self):
        self.device.app_stop(self.APP_NAME)

    def screenshot(self, image_path):
        self.device.screenshot(image_path)

    def run_task(self):
        counter = 0
        while True:
            counter += 1
            time.sleep(3)
            print("拼多多自动视频， 刷了%s个视频" % str(counter))
            self.scroll_download()

    def scroll_up(self):
        self.device(scrollable=True).scroll.backward()

    def scroll_download(self):
        self.device(scrollable=True).scroll.forward()
